package com.admin.user.controller;


import com.admin.user.service.ArUserService;

import com.client.common.service.SmsService;
import com.client.model.pojo.user.ArUser;
import com.client.model.system.*;
import com.client.model.vo.common.Result;
import com.client.model.vo.common.ResultCodeEnum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/airui/api/user")
public class IndexController {

    @Autowired
    private ArUserService arUserService;
    @Autowired
    private SmsService smsService;
    /***
     * 手机号验证码登录
     */
    @PostMapping("/phone")
    public Result phoneLogin(@RequestBody PhoneDto phoneDto){
        return Result.build(arUserService.phoneLogin(phoneDto),ResultCodeEnum.SUCCESS);
    }
    /***
     *忘记密码根据身份信息进行判断
     */
    @PostMapping("/forget")
    public Result forget(@RequestBody ArUser arUser){
        return Result.build(arUserService.forget(arUser),ResultCodeEnum.SUCCESS);
    }
    /***
     * 修改密码
     */
    @PostMapping("/updatePass")
    public Result updatePassword(@RequestBody PassDto passDto){
        return arUserService.updatePass(passDto);
    }


    /**
     * 判断该手机号是否注册
     */
    @GetMapping("/haveMobile")
    public Result haveMobile(Integer mobile){
        return Result.build(arUserService.haveMobile(mobile),ResultCodeEnum.SUCCESS);
    }
    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody ArUser arUser) {
       return arUserService.register(arUser)?
               Result.build(null,ResultCodeEnum.SUCCESS):
               Result.build(null, ResultCodeEnum.DATA_ERROR);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/send")
    public Result phone(@RequestParam("phone") String phone) {
        System.err.println(phone);
       smsService.send(phone);
       return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取验证码接口
     * @return
     */
    @GetMapping("/getValidateCode")
    public Result<ValidateCodeVo> getValidateCode(){
        ValidateCodeVo validateCodeVo = arUserService.getValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){

        LoginVo loginVo = arUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);

    }
    //根据token从redis中取出user信息 赋值
    @GetMapping("/userinfo")
    //map<String,String>
    public Result<LoginUserVo> getUserinfo(@RequestHeader("token")String token){
//        String token = request.getHeader("token");
        LoginUserVo userInfoVo = arUserService.getUserInfo(token);
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);

    }
    //用户退出
    @GetMapping("/logout")
    public Result<String> logout(@RequestHeader("token")String token){
        arUserService.logout(token);
        return Result.build("退出成功", ResultCodeEnum.SUCCESS);
    }

}
